<?php

namespace app\admin\controller;

use app\common\Helper;

use think\Request;

class Storemessage extends Base
{

    public function getlist(){
        $params = $this->request->param();
        $where = [];
        if(isset($params['begin_time']) && $params['begin_time']){
            $where['createDate'] = ['egt', $params['begin_time']];
        }
        if(isset($params['end_time']) && $params['end_time']){
            $where['createDate'] = ['lt', $params['end_time'] . ' 23:59:59'];
        }
        if(isset($params['begin_time']) && $params['begin_time'] && isset($params['end_time']) && $params['end_time']){
            $where['createDate'] = [['egt', $params['begin_time']], ['lt', $params['end_time'] . ' 23:59:59'], 'and'];
        }
        if(isset($params['title']) && $params['title']){
            $where['title'] = ['like', "%{$params['title']}%"];
        }

        $model = model('Storemessage');
        $list = $model->where($where)->order('createDate desc')->paginate(10);
        $total = $model->where($where)->count();

        foreach ($list as $item) {
            if($item['type'] == 1){
                $item['toObject'] = \app\admin\model\Store::where('id', $item['storeId'])->value('name');
            }else{
                $item['toObject'] = '全部店铺';
            }
        }

        return $this->fetch('',[
            'datas' => $list,
            'total' => $total,
            'formget' => $_GET
        ]);
    }

    public function add(){
        return $this->fetch();
    }

    public function edit(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return show(config('status.error_code'),'缺少参数','缺少参数');
        }
        $info = \app\admin\model\Storemessage::get($param['id']);

        return $this->fetch('',[
            'info' => $info
        ]);
    }

    public function savePost(Request $request){
        if($request->isPost()){
            $param = $request->param();

            if(empty($param['id'])){
                //添加
                $model = new \app\admin\model\Storemessage();
                $id = Helper::getUUID();
                $model->id = $id;
                $model->createOper = $this->admin->id;
            }else{
                //修改
                $model = \app\admin\model\Storemessage::get($param['id']);
                if(!$model){
                    return show(config('status.error_code'),'数据异常','数据异常');
                }
                $model->updateOper = $this->admin->id;
            }
            $model->title = $param['title'];
            $model->storeId = $param['storeId'];
            $model->type = $param['type'];
            $model->content = $param['content'];
            $result = $model->save();

            if($result !== false){
                return show(config('status.success_code'),'保存成功','保存成功');
            }else{
                return show(config('status.error_code'),'保存失败','保存失败');
            }
        }else{
            return show(config('status.error_code'),'请求不合法','请求不合法');
        }
    }

    /**
     * 获取店铺
     */
    public function getStore(Request $request){
        $model = model('Store');
        if($request->isPost()){
            $where = [
                'status' => 1
            ];

            $result = $model->where($where)->field('id, name')->select();

            return json($result);
        }
    }

}